iT邦幫忙

2025 iThome 鐵人賽

DAY 24
0
自我挑戰組

資料視覺化的探索之旅:從 ggplot2 技術到視覺化設計系列 第 24

承先啟後:ggplot2 基礎回顧與 Extensions 的必要性

  • 分享至 

  • xImage
  •  

這系列文章截至目前為止,一步步從 ggplot2 的基礎圖形開始,期望分享ggplot2架構與資料視覺化的廣度與多樣性

過程中,不只是單純「會畫圖」,且分享如何挑選合適的圖表來表達資料故事。


回顧:前二十幾天的圖表探索

以下表格整理了我們已經介紹過的 主要 geom_xx 圖層 與其用途:

ggplot2 基本架構程式

# 載入套件
library(ggplot2)

# 基本架構
ggplot(data = <DATA>,                     
       aes(x = <X變數>, y = <Y變數>)) +  
  geom_<幾何圖層>() +                     
  scale_x_continuous() +                  
  facet_wrap(~ <分面變數>) +              
  theme_minimal() +                       
  labs(title = "圖表標題",                 
       x = "X軸標籤",
       y = "Y軸標籤")

ggplot2 常見幾何圖層與應用

類型 代表語法 主要用途 / 特色
散點圖 geom_point() 呈現兩個連續變數間的關係。
抖動圖 geom_jitter() 解決點重疊問題,讓分布更清楚。
點密度圖 ggpointdensity::geom_pointdensity() 以顏色強調密度高低。
折線圖 geom_line() 顯示時間序列或有序資料趨勢。
平滑線 geom_smooth() 添加回歸線或趨勢線。
直方圖 geom_histogram() 呈現連續數據的分布。
密度圖 geom_density() 平滑分布,補充直方圖不足。
頻數多邊形 geom_freqpoly() 以線條替代直方圖,便於比較。
盒鬚圖 geom_boxplot() 展示五數摘要與離群值。
小提琴圖 geom_violin() 呈現完整分布形狀,可加分位數線。
長條圖 geom_col() 類別數據的加總或聚合呈現。
計數條 geom_bar() 自動計數的長條圖。
熱圖 geom_tile() 二維交叉比較,用顏色表示大小。
圓餅 / 甜甜圈圖 geom_bar() + coord_polar() 類別占比的特殊呈現。
標籤 geom_text() / geom_label() 在圖上加註文字,輔助解讀。

除了幾何圖層,同時也採用了 輔助與修飾的語法

  • 位置調整position_jitter()position_dodge()position_nudge()
  • 比例尺scale_x_continuous(labels=scales::comma)scale_fill_xx()scale_colour_xx()
  • 主題theme() 微調,或用 from_theme() 統一設定
  • 分面facet_wrap()facet_grid() 幫助多組比較
  • 座標系coord_flip()coord_polar() 提供不同視角
  • 標註與導引annotate()guides()after_stat()

這些構成了 ggplot2 的核心能力,讓我們能靈活應對大部分的基礎需求。


為什麼需要 Extensions?

雖然 ggplot2 本身功能強大,但在實務中常會遇到一些限制,以下場景就需要 ggplot2 extensions 的幫助:

  • 想要 動畫效果,但 ggplot2 本身僅能輸出靜態圖。
  • 想要 智慧標籤,避免標籤重疊,但內建 geom_text() 無法處理複雜排版。
  • 想要 多圖拼接統計檢定整合,基礎 ggplot2 語法較繁瑣。
  • 想要更「故事化」的圖表,例如 文字沿曲線排列自動凸顯重點數據

Extensions 可以理解成「擴充包」,它們不是取代 ggplot2,而是 在原有框架上,提供更便捷或更進階的功能


引入新的資料情境:以Spotify資料為例

接下來的篇章,我將以 Spotify 提供的音樂特徵資料(Spotify Audio Features) 作為案例,搭配 ggplot2 extensions 展示新的可能性。

這份資料集包含了歌曲的多種屬性,例如:

  • popularity:歌曲熱門程度 (0–100)。
  • danceability:是否適合跳舞 (0–1)。
  • energy:能量感 (0–1)。
  • valence:情感積極度 (0–1)。
  • speechiness:語音比例 (0–1)。
  • tempo:歌曲速度 (BPM)。
  • loudness:響度 (dB)。

這些數值本身就很適合做資料視覺化,特別是透過 ggplot2 extensions,我們能從新的角度來探索音樂的特徵與差異。


小結

今天這篇文章是 承先啟後的關鍵一篇:

  • 回顧了前二十幾天的基礎 geom 與輔助語法,整理成一張完整清單。
  • 提出 ggplot2 extensions 的必要性,作為接下來篇章的過渡。
  • 引入 Spotify 音樂特徵資料,為後續示範鋪路。

接下來,將會挑選一些適合的 extensions,實際示範如何讓視覺化更具延展性與故事感。


🔎 English Abstract

This article serves as a bridge in my Ironman series. Over the past 20+ days, I have explored a wide range of ggplot2 geoms—scatter plots, histograms, density plots, boxplots, violin plots, bar charts, heatmaps, pie charts—and supporting tools such as scales, themes, facets, positions, and annotations. Together, these form the foundation of data visualization in ggplot2.

However, real-world scenarios often demand more: animated graphics, smarter label placement, statistical integration, or flexible multi-plot layouts. This is where ggplot2 extensions come into play. They expand ggplot2’s capabilities while keeping the same grammar of graphics philosophy.

In the upcoming articles, I will introduce Spotify Audio Features as a new dataset, which describes each track’s characteristics such as popularity, energy, danceability, valence, loudness, and tempo. With this dataset, we will explore ggplot2 extensions to create more powerful, flexible, and storytelling-oriented visualizations.


上一篇
熱圖應用(heatmap):從人數到比例,觀察血壓與生活型態的關聯
下一篇
Extension 系列:用 geom_dotsinterval() 觀察趨勢
系列文
資料視覺化的探索之旅:從 ggplot2 技術到視覺化設計25
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言